﻿using System;
using System.ComponentModel;
using System.Web.UI;
namespace TA.Turbo.Controls
{

	/// <summary>
	/// HTML hyper link which acts as a button, rendered as an anchor tag with onclick attribute containing logic to raise a server-side event.
	/// </summary>
	[PersistChildren(true), ParseChildren(false)]
	public class LinkButton : BaseButton
	{
		#region PUBLIC
		#region INIT
		/// <summary>
		/// Ctor.
		/// </summary>
		public LinkButton()
		{
			base.Tag = HtmlTextWriterTag.A;
		}
		#endregion
		#region PROPS
		#region DEF
		/// <summary>
		/// Props.
		/// </summary>
		protected static new Properties _clientProperties = Define(BaseButton._clientProperties,
			new[] {
				new UrlPropertyDefinition("Href", "H", null) { IsUsedByClient = false, },
			});
		/// <summary>
		/// Props.
		/// </summary>
		public override Properties ClientProperties
		{
			get
			{
				return LinkButton._clientProperties;
			}
		}
		#endregion
		/// <summary>
		/// The HREF attribute to be rendered when JavaScript is not confirmed true.
		/// </summary>
		public string Href
		{
			get
			{
				return GetProperty<string>("Href");
			}
			set
			{
				ViewState["Href"] = value;
			}
		}
		[DefaultValue(HtmlTextWriterTag.Unknown)]
		public override HtmlTextWriterTag Tag
		{
			get
			{
				return base.Tag;
			}
			set
			{
				base.Tag = value;
			}
		}
		#endregion
		#region EVENTS
		[PersistenceMode(PersistenceMode.Attribute)]
		public override ServerEvent<EventArgs> Click
		{
			get
			{
				return base.Click;
			}
		}
		#endregion
		#region  EVENTS
		/// <summary>
		/// Raised when this control is clicked. Use this because Click-Server notation is not available (ASP.NET parser shortcoming).
		/// </summary>
		public event EventHandler ServerClick;
		#endregion
		#endregion
		#region INTERNAL
		#region METHODS
		#region INIT
		/// <summary>
		/// Init.
		/// </summary>
		/// <param name="e"></param>
		protected override void OnInit(EventArgs e)
		{
			base.OnInit(e);

			if (this.ServerClick != null)
				base.Click.Server += new EventHandler<EventArgs>(this.ServerClick);
		}
		/// <summary>
		/// Registers LinkControl.js.
		/// </summary>
		protected internal override void RegisterIncludes(IncludeCollection includes)
		{
			base.RegisterIncludes(includes);

			includes.Add(new ScriptResourceInclude("TA.Turbo.Res.JS.Simple.LinkControl.js", typeof(BaseCommandControl)));
		}
		#endregion
		#region RENDER
		/// <summary>
		/// Adds the HREF attribute if this control is enabled.
		/// </summary>
		/// <param name="writer"></param>
		protected override void AddAttributesToRender(HtmlTextWriter writer)
		{
			if (this.Enabled)
			{
				writer.AddAttribute(HtmlTextWriterAttribute.Href, this.Href ?? "");
			}
			base.AddAttributesToRender(writer);
		}
		/// <summary>
		/// Render the contents (which is the string contained by the <see cref="BaseButton.Text"/> property).
		/// </summary>
		/// <param name="writer"></param>
		protected override void RenderContents(HtmlTextWriter writer)
		{
			base.RenderContents(writer);

			writer.Write(base.Text);

			if (this.get_IsDesignMode())
			{
				if (string.IsNullOrEmpty(this.Text))
					writer.Write(this.GetType().Name);
			}
		}
		#endregion
		#region HANDLERS
		/// <summary>
		/// Raises the Click event.
		/// </summary>
		/// <param name="e"></param>
		protected override void OnClick(EventArgs e)
		{
			if (this.ServerClick != null)
			{
				this.ServerClick(this, e);
			}
		}
		#endregion
		#endregion
		#endregion

	}
}

